import java.util.*;

public class Solution {

    public boolean myWay(int[] arr) {

        int size = arr.length - 1;
        int num = arr[size];

        if (size == 0)
            return true;

        int i = 0;
        for ( ; i < size; i++)
            if(arr[i] > num) 
                break;
        
        int j = i;

        for ( ; j < size; j++)
            if (arr[j] < num)
                return false;
        
        if (i == 0 || i == size)
            return myWay(Arrays.copyOf(arr, size));
            
        int[] arr1 = new int[i];
        int[] arr2 = new int[size - i];
        System.arraycopy(arr, 0, arr1, 0, i);

        System.arraycopy(arr, i, arr2, 0, size - i);

        return myWay(arr1) && myWay(arr2);
    }

    public boolean VerifySquenceOfBST(int [] sequence) {
        
        if (sequence.length == 0)
            return false;

        return myWay(sequence);

    }
}